<?php
namespace app\admin\controller\finance;
use app\admin\controller\Controller;

use app\admin\model\dividend\Dividend as DividendModel;
use think\Db;
/**
 * 渠道公司管理
 */
class Apply extends Controller{
    /**
     * 首页
     */
    public function index(){
        $model = new DividendModel();
        return $this->fetch('index', [
            //渠道公司列表
            'list' => $list = $model->getList($this->request->param(),2),
        ]);
    }
    /**
     * 添加渠道公司
     */
    public function add(){
        $model = new DividendModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        $admin=$this->postData('admin');
        // 新增记录
        /*
         * array(4) {
              ["name"]=>
              string(18) "自营公司名称"
              ["images"]=>
              string(1) "6"
              ["legalName"]=>
              string(6) "郭涛"
              ["phone"]=>
              string(11) "15109287068"
            }
         */
        if($model->add($this->postData('company'),$admin)){
            return $this->renderSuccess('添加成功', url('finance.apply/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    /**
     * 编辑渠道公司信息
     */
    public function edit($id){
        //公司详情
        $model = DividendModel::detail($id);
        //登录信息
        $loginInfo=Db::name('yonghui_adminuser')->where(['companyId'=>$model->id])->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model','loginInfo'));
        }
        //更新记录
        if ($model->edit($this->postData('company'))){
            return $this->renderSuccess('更新成功', url('dividend.system/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 删除渠道公司
     */
    public function delete($id){
        $model = DividendModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    /**
     * 恢复信息
     */
    public function recover($id){
        $model = DividendModel::detail($id);
        if (!$model->setrecover()) {
            return $this->renderError($model->getError() ?: '恢复失败');
        }
        return $this->renderSuccess('恢复成功');
    }
}